import 'dart:async';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../../../m_chatroom.dart';
import '../../interface/chat_room_leaf_interface.dart';

class DoufengTimeCheckWidgetModel extends ChatRoomChangeNotifier {
  String textTimeLast = '';
  Timer? _timer;
  BuildContext context;
  var price = 0;

  DoufengTimeCheckWidgetModel(this.context);

  @override
  onRootProxyModelCreated() {
    _initData();
  }

  @override
  observeMessage(int messageId, data) {}

  @override
  void dispose() {
    _timer?.cancel();
    _timer = null;
    super.dispose();
  }

  // int get now => GlobalConfig.serverTime;

  _initData() async {
    // int timeEnd = now + 8 * 60 * 1000;
    // timeEnd = (MessageNotifierListenerRidePush.rideEndTime) > 0
    //     ? MessageNotifierListenerRidePush.rideEndTime
    //     : timeEnd;
    // int seconds = (timeEnd - now) ~/ 1000;
    // textTimeLast = T.current!.car_remaining_some_duration(
    //     _twoDigits(seconds ~/ 60), _twoDigits(max(seconds, 0) % 60));
    // _startTimer();
  }

  Future<void> loadPrice() async {
    // var user = roomModel.channel?.broadcaster
    //     .firstWhereOrNull((_) => _.micType == MicTypeEnum.COMMON_MIC);
    // if (user == null) return;
    // var result = await AutoRideApi.instance.getPrice(user.user.id.toInt());
    // if (result.isSuccess) {
    //   price = result.data?.ridePrice.toInt() ?? 0;
    // }
  }

  void _startTimer() {
    _timer?.cancel();

    // // 默认8分钟
    // int timeEnd = now + 8 * 60 * 1000;

    // _timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
    //   timeEnd = (MessageNotifierListenerRidePush.rideEndTime) > 0
    //       ? MessageNotifierListenerRidePush.rideEndTime
    //       : timeEnd;
    //   int seconds = (timeEnd - now) ~/ 1000;

    //   // 延迟5秒退出房间
    //   if (seconds < -5) {
    //     _timer?.cancel();
    //     roomModel.roomPageModel?.disposePageAndData();
    //     return;
    //   }
    //   textTimeLast = T.current!.car_remaining_some_duration(
    //       _twoDigits(seconds ~/ 60), _twoDigits(max(seconds, 0) % 60));

    //   notifyListeners();
    // });
  }

  static String _twoDigits(int n) {
    if (n >= 10) return "$n";
    return "0$n";
  }

  renew(BuildContext context) async {
    if (price <= 0) {
      await loadPrice();
    }
    // bool trust = CacheUtil.getBool(
    //     '${UserManager.instance.currentUid}.trust.renew.ride', false);
    // if (!trust) {
    //   var sure = await JRDialog.showCommonDialog(
    //       context: Get.context!,
    //       data: TextWithCheckDialogData(
    //           title: T.current!.df_continue_time,
    //           desc: T.current!.confirm_continue_time_tips(price),
    //           checked: true,
    //           checkSetter: (value) {
    //             CacheUtil.setBool(
    //                 '${UserManager.instance.currentUid}.trust.renew.ride',
    //                 value);
    //           }));
    //   if (sure != true) return;
    // }

    // var renewRsp =
    //     await handleRsp(() => AutoRideApi.instance.renew(RenewReq.create()));
    // if (!renewRsp.isSuccess && renewRsp.errorCode == 1001510100) {
    //   RechargeDialog.showWithBottomSheet(context,
    //       price: price, source: ChargeSource.RIDE_1V1, onRechargeSuccess: () {
    //     //充值成功，刷新数据
    //   });
    // }
  }
}
